<template>
  <div class="execution">
    <basic-container>
      <avue-crud v-model="form" v-bind="bindVal" v-on="onEvent" :page.sync="page"
        :permission="permissionList"
        :before-open="beforeOpen">
        <template slot="menuLeft">
          <el-button type="primary" size="small" @click="handleExport">导 出</el-button>
          <el-button type="primary" size="small" :hidden="true" v-if="false" @click="handlePrint">打 印</el-button>
        </template>
        <template slot="swTkTicketDeliveryDetailsForm">
          <dynamic-crud title="配入明细信息" :data="form.swTkTicketDeliveryDetails"
            :option="dynamicOption" :button="false">
            <template slot="receiveCount" slot-scope="{row}">
              <avue-input v-if="row.$cellEdit" v-model.number="row.receiveCount"
                size="small"
                :controls="false"
                @change="onReceiveCountChange(row)">
              </avue-input>
              <span v-else>{{row.receiveCount}}</span>
            </template>
          </dynamic-crud>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
  import baseCrud from '@/mixins/base-crud'
  import {confirmOrFeedBack,exportExcel} from '@/api/subwaystm/swstticketin'
  import {getDetailObj} from "@/api/subway/swtkticketdelivery"
  import {dateFormat} from '@/util/date'
  import {validatenull} from "@/util/validate";
  import { startDate, endDate } from "@/const/crud/subway/swtkticketreceive";
  import { handleExcelResponse } from "@/util/util";
  import store from "@/store";

  export default {
    name: 'swstticketin',
    mixins: [baseCrud('subwaystm/swstticketin')],
    data() {
      return {
        searchForm: {
          dates1: [startDate, endDate]
        },
        dynamicOption: {
          column: [{
            label: '票种',
            prop: 'ticketType',
            type: 'select',
            dicUrl: '/support/swpmticketcardtype/dict',
            props: {
              label: 'mainName',
              value: 'mainEnName'
            }
          }, {
            label: '车票名称',
            prop: 'ticketTypeParam',
            type: 'select',
            dicUrl: '/support/swpmticketcardtypeparam/dictAll/{{key}}',
            props: {
              label: 'subName',
              value: 'subEnName'
            }
          }, {
            label: '号段',
            prop: 'numberInterval'
          }, {
            label: '卡号',
            prop: 'cardNumber'
          }, {
            label: '金额',
            prop: 'ticketAmount',
            type: 'number',
            precision: 2
          }, {
            label: '次数',
            prop: 'ticketCount'
          }, {
            label: '配入数量',
            prop: 'deliveryCount'
          }, {
            label: '接收张数',
            prop: 'receiveCount',
            type: 'number',
            slot: true,
            cell: true
          }, {
            label: '差异张数',
            prop: 'differenceCount'
          }, {
            label: '备注',
            prop: 'remark',
          }]
        }
      }
    },
    computed: {
      permissionList() {
        return {
          addBtn: this.vaildData(this.permissions.stm_sw_st_ticket_in_add, false),
          delBtn: this.vaildData(this.permissions.stm_sw_st_ticket_in_del, false),
          editBtn: this.vaildData(this.permissions.stm_sw_st_ticket_in_edit, false)
        };
      }
    },
    methods: {
      async handleExport(){
        let loading = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)"
        })
        try {
          let tableSelect = this.$refs.crud.tableSelect;
          let ids = tableSelect.map(row => row.id)
          let param = {
            ids: this.validatenull(ids)?null:ids
          };
          const res = await exportExcel(param);
          handleExcelResponse(res);
        } finally {
          loading.close();
        }
      },
      rowUpdate: function(row, index, done, loading) {
        confirmOrFeedBack(this.form).then(data => {
          this.$message.success('修改成功')
          done()
          this.getList(this.page)
        }).catch(() => {
          loading();
        });
      },
      beforeOpen(done, type){
        if(type != 'add'){
          getDetailObj(this.form.id).then(response => {
            this.form.swTkTicketDeliveryDetails = response.data.data.swTkTicketDeliveryDetails
            if(type == 'edit'){
              this.form.swTkTicketDeliveryDetails.forEach(d => {
                d.$cellEdit = true
              })
              if(validatenull(this.form.receivePersonName)||validatenull(this.form.receivePersonCode)){
                this.form.receivePersonName = store.getters.userInfo.nikename;
                this.form.receivePersonCode = store.getters.userInfo.userCode;
              }
              let receivePersonName = this.findObject(this.tableOption.group[1].column, 'receivePersonName')
              let receiveTime = this.findObject(this.tableOption.group[1].column, 'receiveTime')
              let feedback = this.findObject(this.tableOption.group[2].column, 'feedback')
              let stationSurveyResult = this.findObject(this.tableOption.group[3].column, 'stationSurveyResult')
              // 配入状态 0:待确认;1:已确认;2:差异反馈;3:差异申诉
              //receivePersonName.editDisabled = (this.form.deliveryStatus != '0')
              receiveTime.editDisabled =  (this.form.deliveryStatus != '0')
              feedback.editDisabled = (this.form.deliveryStatus != '0')
              stationSurveyResult.editDisabled = (this.form.deliveryStatus != '3')
              if(!receiveTime.editDisabled){
                setTimeout(() => {
                  this.form.receiveTime = dateFormat(new Date())
                })
              }
            }
            done()
          })
        }else{
          done()
        }
      },
      onReceiveCountChange(row){
        if(!this.validatenull(row.receiveCount)){
          row.differenceCount = row.deliveryCount - row.receiveCount
        }else{
          row.differenceCount = null
        }
      }
    }
  }
</script>
